<?php
	// tạo các biến cần dùng
	$MaLoai = $_REQUEST["MaLoai"];
	$MaNCC = $_REQUEST["MaNCC"];
	$Search = $_REQUEST["Search"];
	$List = $_REQUEST["List"];
	
    if (isset($MaLoai) && isset($MaNCC)) // tìm kiếm theo mã loại và nhà cung cấp
    {
        $_SESSION["Sql"] = "SELECT * FROM HangHoa hh
        					WHERE MaLoai=$MaLoai AND MaNCC='$MaNCC'";
    }
    else if (isset($MaLoai)) // tìm kiếm theo mã loại
    {
        $_SESSION["Sql"] = "SELECT * FROM HangHoa hh
        					WHERE MaLoai=$MaLoai";
    }
    else if (isset($MaNCC))// Tìm kiếm theo nhà cung cấp
    {
        $_SESSION["Sql"] = "SELECT * FROM HangHoa hh
        					WHERE MaNCC='$MaNCC'";
    }
    else if (isset($Search))// nếu có từ khóa thì tìm kiếm theo từ khóa
    {
        $_SESSION["Sql"] = "SELECT hh.* FROM HangHoa hh
        					JOIN Loai lo ON lo.MaLoai=hh.MaLoai
        					JOIN NhaCungCap ncc ON ncc.MaNCC=hh.MaNCC
        					WHERE CONCAT(TenHH, TenLoai, TenCongTy) LIKE '%$Search%'";
    }
    else if (isset($List))
    {
        if($List == 'BEST')
        {
        	$_SESSION["Sql"] = "SELECT hh.MaHH, TenHH, hh.DonGia, Hinh, SUM(ct.DonGia*ct.SoLuong*(1-ct.GiamGia)) As DS
        						FROM HangHoa hh
        						JOIN ChiTietHD ct ON ct.MaHH=hh.MaHH
        						GROUP BY hh.MaHH, TenHH, hh.DonGia, Hinh
        						HAVING DS > 0
        						ORDER BY DS DESC";
        }
        else if($List == 'VIEWED')
        {
        	$_SESSION["Sql"] = "SELECT * FROM HangHoa
        						ORDER BY SoLanXem DESC";
        }
        else if($List == 'LATEST')
        {
        	$_SESSION["Sql"] = "SELECT * FROM HangHoa
        						ORDER BY NgaySX DESC";
        }
        else if($List == 'DISCOUNTED')
        {
        	$_SESSION["Sql"] = "SELECT * FROM HangHoa
        						ORDER BY GiamGia DESC";
        }
        else if($List == 'MARKED_ALL')
        {
        	$_SESSION["Sql"] = "SELECT hh.MaHH, TenHH, DonGia, Hinh, COUNT(yt.MaHH) As SL
        						FROM HangHoa hh
        						JOIN YeuThich yt ON yt.MaHH=hh.MaHH
        						GROUP BY hh.MaHH, TenHH, DonGia, Hinh
        						HAVING SL > 0
        						ORDER BY SL DESC";
        }
        else if($List == 'SENT_ALL')
        {
        	$_SESSION["Sql"] = "SELECT hh.MaHH, TenHH, DonGia, Hinh, COUNT(bb.MaHH) As SL
        						FROM HangHoa hh
        						JOIN BanBe bb ON bb.MaHH=hh.MaHH
        						GROUP BY hh.MaHH, TenHH, DonGia, Hinh
        						HAVING SL > 0
        						ORDER BY SL DESC";
        }
        else if(isset($_SESSION['KhachHang']))
        {
        	$KhachHang = $_SESSION['KhachHang'];
	        
	        if($List == 'SENT')
	        {
	        	$_SESSION["Sql"] = "SELECT DISTINCT hh.* FROM HangHoa hh
	        						JOIN BanBe bb ON bb.MaHH=hh.MaHH
	        						WHERE MaKH='$KhachHang[MaKH]'";
	        }
	        else if($List == 'FAVORITE')
	        {
	        	$_SESSION["Sql"] = "SELECT DISTINCT hh.* FROM HangHoa hh
	        						JOIN YeuThich yt ON yt.MaHH=hh.MaHH
	        						WHERE MaKH='$KhachHang[MaKH]'";
	        }
	        else if($List == 'PURCHASED')
	        {
	        	$_SESSION["Sql"] = "SELECT DISTINCT hh.* FROM HangHoa hh
	        						JOIN ChiTietHD ct ON ct.MaHH=hh.MaHH
	        						JOIN HoaDon hd ON hd.MaHD=ct.MaHD
	        						WHERE MaKH='$KhachHang[MaKH]'";
	        }
        }
    }
    
    if(!isset($_SESSION["Sql"]))
    {
    	$_SESSION["Sql"] = "SELECT * FROM HangHoa";
    }
?>

<?php
//----- PHAN TRANG -----//
	$sql = "SELECT COUNT(*) AS SoLuong FROM (".$_SESSION["Sql"].") AS MySql";
	$rows = mysql_query($sql);
	$row = mysql_fetch_array($rows);
	$RowCount = $row['SoLuong'];
	
	$PageSize = 6;
	$PageCount = ceil($RowCount/$PageSize);
	$LastPageNo = $PageCount - 1;
	
	if(isset($_GET['PageNo']))
	{
		$_SESSION['PageNo'] = $_GET['PageNo'];
	}
	
	$PageNo = isset($_SESSION['PageNo']) ? $_SESSION['PageNo'] : 0;
	
	if($PageNo < 0)
	{
		$PageNo = $_SESSION['PageNo'] = 0;
	}
	else if($PageNo > $LastPageNo)
	{
		$PageNo = $_SESSION['PageNo'] = $LastPageNo;
	}
	$StartRow = $PageNo * $PageSize;
	
	$GroupSize = 5;
	$GroupNo = floor($PageNo/$GroupSize);
	$GroupCount = ceil($PageCount/$GroupSize);
	$LastGroupNo = $GroupCount - 1;
	$StartPage = $GroupNo*$GroupSize;
	if($GroupNo == $LastGroupNo)
	{
		$EndPage = $LastPageNo;
	}
	else
	{
		$EndPage = $StartPage + $GroupSize - 1;
	}
	
	//----- KET THUC PHAN TRANG -----//
?>


<?php
$sql = $_SESSION["Sql"] . " LIMIT $StartRow, $PageSize";
$rows = mysql_query($sql);
for($index=1;$row = mysql_fetch_array($rows);$index++)
{
	$MaHH = $row['MaHH'];
	$TenHH = $row['TenHH'];
	$DonGia = $row['DonGia'];
	$Hinh = $row['Hinh'];
	$MoTa = substr($row['MoTa'],0,70);
?>
  <div class="product">
  	<div class="product_pic"><a href="?p=ProductDetail&MaHH=<?php echo $MaHH?>">
					<img src="Hinh/HangHoa/<?php echo $Hinh?>" width="150" height="150">
				</a></div>
    <div class="product_title" style="height:40px"><a href="?p=ProductDetail&MaHH=<?php echo $MaHH?>"> <?php echo $TenHH?></a></div>
    <div class="product_desc"><?php echo $MoTa?> ... 
    <a href="?p=ProductDetail&MaHH=<?php echo $MaHH?>"><br /><div align="right">Chi tiết ></div></a><p>
			      </a>
				  
		    </p></div>
    <div class="price"><?php echo $DonGia?> VND <a href="?p=ProductList&btnAddToCart=<?php echo $MaHH?>">
				    <img hspace="5px" src="Hinh/icons/Add To Basket.png"/>
			      </a></div>    
  </div>
<?php }?>    

<p><br />
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><br />
<div class="clean0"></div>  
 <div style="width:97%">
<hr>
<?php
echo "<a href='?p=ProductList&PageNo=0'>|<< </a> <a href='?p=ProductList&PageNo=".($StartPage-1)."'>Prev</a>";
for($i=$StartPage;$i<=$EndPage;$i++)
{
	echo " <a href='?p=ProductList&PageNo=".($i)."'>".($i+1)."</a> ";
}
echo "<a href='?p=ProductList&PageNo=".($StartPage + $GroupSize + 1)."'>Next</a> <a href='?p=ProductList&PageNo=<?php echo $LastPageNo?>'> >>|</a>";
?><br/> <?php echo "Page ".($PageNo+1)." of ".$PageCount." pages. "?>
</div>